/// @tags:
#include <cstdio>
#include <iostream>
#include <vector>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef long long LL;

constexpr int MAXN = 2.5e3 + 3, MAXM = MAXN * 2, P = 1e9 + 7;

int n, m, ans;
int siz[MAXN], fac[MAXM], ifac[MAXM], xp[MAXN][MAXM];

struct Edge {
  int v, w;
};
std::vector<Edge> G[MAXN];

inline void add(int u, int v, int w) { G[u].push_back(Edge{v, w}), G[v].push_back(Edge{u, w}); }

inline LL qpow(LL base, int exp = P - 2) {
  LL res = 1;
  for (; exp; exp >>= 1, base = base * base % P)
    if (exp & 1) res = res * base % P;
  return res;
}

inline int inc(int x, int y) {
  x += y;
  return x >= P ? x - P : x;
}

inline void prework() {
  fac[0] = 1;
  for (int i = 1; i <= m * 2; ++i) fac[i] = (LL)fac[i - 1] * i % P;
  ifac[m * 2] = qpow(fac[m * 2]);
  for (int i = m * 2 - 1; i >= 0; --i) ifac[i] = (LL)ifac[i + 1] * (i + 1) % P;
  for (rint i = 1; i <= n; ++i) {
    xp[i][0] = 1;
    for (rint j = 1; j <= 2 * m; ++j) xp[i][j] = (LL)xp[i][j - 1] * i % P;
  }
}

inline LL C(int n, int m) { return (LL)fac[n] * ifac[m] % P * ifac[n - m] % P; }

void dfs(int u, int fa) {
  siz[u] = 1;
  for (auto &i : G[u]) {
    int v = i.v;
    if (v == fa) continue;
    dfs(v, u);
    siz[u] += siz[v];
    for (int sum = 0; sum <= 2 * m; sum++)
      ans = inc(ans, std::min(sum, 2 * m - sum) * C(m * 2, sum) % P * xp[siz[v]][sum] % P *
                         xp[n - siz[v]][2 * m - sum] % P * i.w % P);
  }
}

inline int main() {
  cin >> n >> m;
  for (int i = 1, u, v, w; i < n; ++i) {
    cin >> u >> v >> w;
    add(u, v, w);
  }
  prework();
  dfs(1, 1);
  cout << ans;
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("class.in", "r", stdin);
  freopen("class.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
